import { EPosition, IHero } from "../typings/types/hero";
import { positionZnMap, typeZnMap } from "../contants/hero";

// 英雄构造类
export default class HeroHelper {
  // id
  id: number = 0;
  // 名字
  name: string = "";
  // 头像
  avatar: string = "";
  // 分路
  positions?: EPosition[] = [];
  // 职业定位
  types?: number[] = [];

  constructor(props: IHero) {
    const {
      id,
      name,
      avatar,
      heroType = 0,
      heroType2 = 0,
      heroPos = 0,
      heroPos2 = 0,
    } = props;
    this.id = id;
    this.name = name;
    this.avatar = this._getAvatar(avatar);
    const { positions, types } = this._getPosition({
      heroTypeList: [heroType, heroType2],
      heroPosList: [heroPos, heroPos2],
    });
    this.positions = positions;
    this.types = types;
  }

  // 获取头像
  private _getAvatar(ename: string) {
    let result: string = `/images/avatars/${ename}.jpg`;
    // https://game.gtimg.cn/images/yxzj/img201606/heroimg/195/195.jpg
    // let result: string = `https://game.gtimg.cn/images/yxzj/img201606/heroimg/${ename}/${ename}.jpg`;

    return result;
  }

  // 获取分路
  private _getPosition(data: any) {
    const { heroPosList, heroTypeList } = data;
    // 职业定位
    const types: any[] = [];
    // 分路
    const positions: any[] = [];

    // 是否有定位字段
    let hasHeroPos: boolean = false;

    heroPosList.forEach((item: number) => {
      if (!item) {
        return;
      }
      hasHeroPos = true;
      // 防重
      if (positions.indexOf(positionZnMap.get(item)) === -1) {
        positions.push(positionZnMap.get(item));
      }
    });
    heroTypeList.forEach((item: number) => {
      if (item) {
        // 如果没有定位 && 防重，则智能分配
        if (!hasHeroPos && positions.indexOf(this._setPosition(item)) === -1) {
          positions.push(this._setPosition(item));
        }
        types.push(typeZnMap.get(item));
      }
    });

    return {
      positions,
      types,
    };
  }

  // 根据type设置分路
  private _setPosition(heroType: number) {
    let result: any;
    switch (heroType) {
      case 1:
        result = EPosition.TOP;
        break;
      case 2:
        result = EPosition.AP;
        break;
      case 3:
        result = EPosition.TOP;
        break;
      case 4:
        result = EPosition.JUG;
        break;
      case 5:
        result = EPosition.AD;
        break;
      case 6:
        result = EPosition.SUP;
        break;
    }
    return result;
  }
}
